package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.sms.SmsHomeRecommendSubjectMapper;
import com.itheima.pojo.sms.SmsHomeRecommendProduct;
import com.itheima.pojo.sms.SmsHomeRecommendSubject;
import com.itheima.pojo.sms.vo.UpdateVo;
import com.itheima.service.sms.SmsHomeRecommendSubjectService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SmsHomeRecommendSubjectServiceImpl extends ServiceImpl<SmsHomeRecommendSubjectMapper, SmsHomeRecommendSubject> implements SmsHomeRecommendSubjectService {
    /**
     * 添加首页推荐专题
     * @param list
     */
    @Override
    public void create(List<SmsHomeRecommendSubject> list) {
        if (list.size()>0){
            for (SmsHomeRecommendSubject smsHomeRecommendSubject : list) {
                if (smsHomeRecommendSubject!=null){
                    this.save(smsHomeRecommendSubject);
                }
            }
        }

    }

    /**
     * 批量删除推荐专题
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        if (ids.size()>0){
            this.removeByIds(ids);
        }
    }

/*    *//**
     * 分页查询推荐
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     *//*
    @Override
    public Page<SmsHomeRecommendSubject> pageList(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {

        Page<SmsHomeRecommendSubject> pageInfo=new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<SmsHomeRecommendSubject> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(recommendStatus!=null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus)
                .like(subjectName!=null,SmsHomeRecommendSubject::getSubjectName,subjectName)
                .orderByDesc(SmsHomeRecommendSubject::getSort);
        Page<SmsHomeRecommendSubject> page = this.page(pageInfo, lambdaQueryWrapper);
        return page;
    }*/
    /**
     * 分页查询推荐
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @param recommendStatus
     * @return
     */
    @Override
    public PageInfo pageList(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {

        //Page<SmsHomeRecommendSubject> pageInfo=new Page<>(pageNum,pageSize);
        PageInfo pageInfo=new PageInfo();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        LambdaQueryWrapper<SmsHomeRecommendSubject> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(recommendStatus!=null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus)
                .like(subjectName!=null,SmsHomeRecommendSubject::getSubjectName,subjectName)
                .orderByDesc(SmsHomeRecommendSubject::getSort);
        //Page<SmsHomeRecommendSubject> page = this.page(pageInfo, lambdaQueryWrapper);
        List<SmsHomeRecommendSubject> list = this.list(lambdaQueryWrapper);
        pageInfo.setList(list);
        int total = this.count(lambdaQueryWrapper);
        pageInfo.setTotal(total);
        Integer totalPage = PageInfo.totalPage(pageSize, total);
        pageInfo.setTotalPage(totalPage);
        return pageInfo;
    }

    /**
     * 批量修改推荐状态
     * @param
     */
    @Override
    public void updateStatus(List<Long> ids, Integer recommendStatus) {
        ids.stream().forEach(item->{
            SmsHomeRecommendSubject recommendSubject = this.getById(item);
            recommendSubject.setRecommendStatus(recommendStatus);
            this.updateById(recommendSubject);
        });
    }


    /**
     * 修改推荐排序
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = this.getById(id);
        if (smsHomeRecommendSubject!=null){
            smsHomeRecommendSubject.setSort(sort);
            this.updateById(smsHomeRecommendSubject);
        }
    }


}
